package com.djp.computerServer.controller;

import com.djp.computerServer.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseController<T extends BaseService> {
    @Autowired
    protected T service;

    private Map map;

    protected Map buildMap(String message, List data) {
        map = new HashMap();
        boolean isSuccess = false;
        int code;
        if (data != null && !data.isEmpty()) {
            code = 200;
            isSuccess = true;
        } else {
            code = 400;
            isSuccess = false;
        }
        map.put("code", code);
        map.put("data", data);
        map.put("message", message + (isSuccess ? "成功" : "失败"));
        return map;
    }

    protected Map buildMap(String message, Boolean data) {
        map = new HashMap();
        boolean isSuccess = false;
        if (data) {
            map.put("code", 200);
            isSuccess = true;
        } else {
            map.put("code", 400);
            isSuccess = false;
        }
        map.put("message", message + (isSuccess ? "成功" : "失败"));
        return map;
    }
}
